package com.xinqi.modules.prepare.prepare.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.ImmutableSet;
import com.xinqi.common.base.annotations.Describe;
import com.xinqi.common.base.enums.BaseEnum;
import lombok.Getter;

import java.util.Collections;
import java.util.Set;

import static com.xinqi.modules.prepare.prepare.enums.PrepareMemberPermissionEnum.下载;
import static com.xinqi.modules.prepare.prepare.enums.PrepareMemberPermissionEnum.分享;
import static com.xinqi.modules.prepare.prepare.enums.PrepareMemberPermissionEnum.删除;
import static com.xinqi.modules.prepare.prepare.enums.PrepareMemberPermissionEnum.复制到;
import static com.xinqi.modules.prepare.prepare.enums.PrepareMemberPermissionEnum.新建;
import static com.xinqi.modules.prepare.prepare.enums.PrepareMemberPermissionEnum.添加到课程;
import static com.xinqi.modules.prepare.prepare.enums.PrepareMemberPermissionEnum.编辑;
import static com.xinqi.modules.prepare.prepare.enums.PrepareMemberPermissionEnum.还原;

/**
 * 备课区成员角色
 * <p>
 * 角色，LEADER-组长；ADMIN-管理员；MEMBER-成员
 * </p>
 *
 * @author linkai
 * @date 2023/03/19
 */
@Describe("备课区成员角色")
public enum PrepareMemberRoleEnum implements BaseEnum {

    LEADER("LEADER", "组长", Collections.emptySet()),
    MEMBER("MEMBER", "成员", ImmutableSet.of(新建, 编辑, 复制到, 分享, 下载, 删除, 还原, 添加到课程)),
    ;

    @Getter(onMethod_ = {@JsonValue})
    private final String code;
    @Getter
    private final String describe;
    @Getter
    private final Set<PrepareMemberPermissionEnum> permissionSet;

    PrepareMemberRoleEnum(String code, String describe, Set<PrepareMemberPermissionEnum> permissionSet) {
        this.code = code;
        this.describe = describe;
        this.permissionSet = permissionSet;
    }

    @JsonCreator
    public static PrepareMemberRoleEnum of(String code) {
        return BaseEnum.of(PrepareMemberRoleEnum.class, code);
    }

    public boolean validate(PrepareMemberPermissionEnum permission) {
        if (this == LEADER) {
            return true;
        }
        return permissionSet.contains(permission);
    }

}
